from rest_framework.generics import ListAPIView, GenericAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response

from goods.models import SPU, SPUSpecification
from meiduo_admin.serializers.spus import SPUSimpleSerializer, SPUSpecSerializer


class SPUSimpleView(ListAPIView):
    permission_classes = [IsAdminUser]
    queryset = SPU.objects.all()
    serializer_class = SPUSimpleSerializer

    pagination_class = None



class SPUSpecView(GenericAPIView):
    permission_classes = [IsAdminUser]

    def get(self, request, pk):
        """
        获取spu规格选项数据:
        1. 根据pk获取spu specs数据
        2. 将spu数据序列化并返回
        """
        # 1. 根据pk获取spu specs数据
        specs = SPUSpecification.objects.filter(spu_id=pk)

        # 2. 将spu数据序列化并返回
        serializer = SPUSpecSerializer(specs, many=True)
        return Response(serializer.data)





















